package com.nbpt;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class HomeActivity extends AppCompatActivity{
    Button button1,button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        getSupportActionBar().setTitle("智能家居");

        button1=findViewById(R.id.bnt_open);
        button2=findViewById(R.id.bnt_close);

        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what==1){
                    Toast.makeText(HomeActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                }else {
                    String errMessage= (String) msg.obj;
                    Toast.makeText(HomeActivity.this, errMessage, Toast.LENGTH_SHORT).show();
                }
            }
        };

        final JSONObject jsonAccount=new JSONObject();
       button1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       HttpURLConnection connection=null;
                       try {
                           URL url=new URL("http://192.168.1.7:9020/iot/class/curtain?action=open&id=2");
                           connection=(HttpURLConnection) url.openConnection();
                           connection.setRequestMethod("POST");
                           connection.setConnectTimeout(5000);
                           connection.setReadTimeout(50000);
                           connection.setRequestProperty("connection","Keep_Alive");
                           connection.setRequestProperty("Charset","UTF-8");
                           connection.setRequestProperty("Content-Type","application/json");
                           OutputStream os=connection.getOutputStream();
                           os.write(jsonAccount.toString().getBytes());
                           os.flush();
                           os.close();
                           if(connection.getResponseCode()==200){
                               InputStream is=connection.getInputStream();
                               BufferedReader reader=new BufferedReader(new InputStreamReader(is));
                               String line;
                               String response="";
                               while ((line=reader.readLine())!=null){
                                   response+=line;
                               }
                               Log.e("MainActivity",response);
                               JSONObject jsonResponse=new JSONObject(response);
                               int code=jsonResponse.getInt("code");
                               if (code==10001){
//                        Toast.makeText(MainActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                                   handler.sendEmptyMessage(1);
                                   Intent intent = new Intent(HomeActivity.this, MenuActivity.class);
                                   startActivity(intent);
                               }else {
                                   String errMessage=jsonResponse.getString("msg");
                                   Message msg=new Message();
                                   msg.what=0;
                                   msg.obj=errMessage;
                                   handler.sendMessage(msg);
//                        Toast.makeText(MainActivity.this, errMessage, Toast.LENGTH_SHORT).show();
                               }
                           }else {
                               Log.e("MainActivity","服务器返回错误！");
                           }

                       } catch (Exception e) {
                           e.printStackTrace();
                       }
                   }
               }).start();
           }
       });
    }
}




//        Thread thread1=new Thread(new Runnable() {
//            @Override
//            public void run() {
//                int i=1;
//                while(true){
//                    Log.e("HomeActivity","取外卖!"+i);
//                    i++;
//                    try{
//                        Thread.sleep(100);
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//        thread1.start();
//        MyThread thread2=new MyThread();
//        thread2.start();
//    }
//    class MyThread extends Thread{
//        @Override
//        public void run() {
//            super.run();
//            int i=1;
//            while(true){
//                Log.e("HomeActivity","写作业!"+i);
//                i++;
//
//                try{
//                    Thread.sleep(100);
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//
//        }
